package com.hnas.ecard.dao.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.core.dao.PageBean;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.util.StringUtil;
import com.hnas.ecard.dao.AccChannelReDao;
import com.hnas.ecard.orm.AccChannelRe;
import com.hnas.ecard.service.dto.DTOAccChannelRe;
import com.hnas.ecard.service.search.DTOEAccChannelReSearch;

@Component("accChannelReDao")
public class AccChannelReDaoImpl extends BaseDaoImpl<AccChannelRe> implements AccChannelReDao {

	public AccChannelReDaoImpl() {
		super(AccChannelRe.class);
	}

	@Override
	public AccChannelRe saveOrUpdate(AccChannelRe vo) {
		return null;
	}

	@Override
	public DTOPagedList<DTOAccChannelRe> page(DTOEAccChannelReSearch criteria) {
		DTOPagedList<DTOAccChannelRe> pageList = new DTOPagedList<DTOAccChannelRe>();
		List<DTOAccChannelRe> list = new ArrayList<DTOAccChannelRe>();
		PageBean<AccChannelRe> page = new PageBean<AccChannelRe>();
		page.setPageNo(criteria.getPageNo());
		page.setPageSize(criteria.getPageSize());
		page.setTotalCount(criteria.getTotalCount());
		StringBuffer sb = new StringBuffer();
		StringBuffer countSb = new StringBuffer();
		List<Object> paras = new ArrayList<Object>();
		int j = 1;
		sb.append(" from tbl_acc_channel_re t1 left join tbl_ecard_channel_info t2 on t1.channel_id = t2.channel_code left join tbl_accinfo t3 on t1.acc_id = t3.id where 1=1 ");
		if(StringUtil.isNotEmpty(criteria.getChannelCode())){
			sb.append(" and t2.channel_code = ?"+j++);
			paras.add(criteria.getChannelCode().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getChannelName())){
			sb.append(" and t2.channel_name = ?"+j++);
			paras.add(criteria.getChannelName().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getPhoneNo())){
			sb.append(" and t3.phone_no = ?"+j++);
			paras.add(criteria.getPhoneNo().trim());
		}
		
		sb.append(" order by t1.create_time desc");
		String sql = "select t1.id,t1.acc_id,t3.acc_name,t2.id as channel_id,t2.channel_code,t2.channel_name,t3.phone_no "+sb.toString();
		String countSql = "select count(1) "+sb.toString();
		Query query = this.entityManager.createNativeQuery(sql);
		Query countQuery = this.entityManager.createNativeQuery(countSql);
		if(paras.size() > 0) {
			for(int i = 1; i <= paras.size(); i++) {
				query.setParameter(i, paras.get(i-1));
				countQuery.setParameter(i, paras.get(i-1));
			}
		}
		List<Object[]> queryList = query.getResultList();
		int count = ((BigInteger)countQuery.getSingleResult()).intValue();
		page.setTotalCount(count);
		pageList.setPageNo(page.getPageNo());
		pageList.setPageSize(page.getPageSize());
		pageList.setTotalCount(page.getTotalCount());
		if(queryList != null && queryList.size() > 0) {
			for(Object[] obj : queryList){
				DTOAccChannelRe dto = new DTOAccChannelRe();
				dto.setId(String.valueOf(obj[0]));
				dto.setAccId(String.valueOf(obj[1]));
				dto.setAccName(String.valueOf(obj[2]));
				dto.setChannelId(String.valueOf(obj[3]));
				dto.setChannelCode(String.valueOf(obj[4]));
				dto.setChannelName(String.valueOf(obj[5]));
				dto.setPhoneNo(String.valueOf(obj[6]));
				list.add(dto);
			}
		}
		pageList.setList(list);
		return pageList;
	}

}
